package handles

import (
	"cargo-x/cache"
	"cargo-x/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {

	car_code := c.PostForm("car_code")
	car_name := c.PostForm("car_name")
	car_style := c.PostForm("car_style")
	data := models.Car{

		CarCode:  car_code,
		CarName:  car_name,
		CarStyle: car_style,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	order_car := c.PostForm("order_car")
	order_from := c.PostForm("order_from")
	order_to := c.PostForm("order_to")
	cid, _ := strconv.Atoi(c.PostForm("cid"))

	data := models.CarOrder{

		OrderCode: order_car,
		OrderFrom: order_from,
		OrderTo:   order_to,
		Cid:       cid,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarOrderList(c *gin.Context) {

	/*
		SELECT
		  car_orders.id AS  OrderId,
		  cars.car_code AS CarCode,
		  cars.car_name AS CarName,
		  cars.car_style AS CarStyle,
		  car_orders.order_code AS OrderCode,
		  car_orders.order_from AS OrderFrom,
		  car_orders.order_to AS OrderTo
		FROM
		  cars
		  LEFT JOIN car_orders ON cars.id = car_orders.cid
	*/

	var list []models.CarOrderList
	redis_key := "car_order_list_"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("cars").
			Select("car_orders.id AS  OrderId",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_style AS CarStyle",
				"car_orders.order_code AS OrderCode",
				"car_orders.order_from AS OrderFrom",
				"car_orders.order_to AS OrderTo",
			).Joins("LEFT JOIN car_orders ON cars.id = car_orders.cid").
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"mag":  "成功",
		"data": list,
	})
	return
}

func CarOrderDetail(c *gin.Context) {

	id := c.Query("id")
	/*
		SELECT
		  car_orders.id AS  OrderId,
		  cars.car_code AS CarCode,
		  cars.car_name AS CarName,
		  cars.car_style AS CarStyle,
		  car_orders.order_code AS OrderCode,
		  car_orders.order_from AS OrderFrom,
		  car_orders.order_to AS OrderTo
		FROM
		  cars
		  LEFT JOIN car_orders ON cars.id = car_orders.cid
	*/

	var list models.CarOrderList
	redis_key := "car_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("cars").
			Select("car_orders.id AS  OrderId",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_style AS CarStyle",
				"car_orders.order_code AS OrderCode",
				"car_orders.order_from AS OrderFrom",
				"car_orders.order_to AS OrderTo",
			).Joins("LEFT JOIN car_orders ON cars.id = car_orders.cid").
			Where("car_orders.id=?", id).Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"mag":  "成功",
		"data": list,
	})
	return
}
